<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Score extends Model
{
    protected $casts = [
        'user_id' => 'integer',
        'value' => 'double',
        'year_id' => 'integer',
    ];

    protected $fillable = [
        'user_id', 'value', 'year_id'
    ];

    protected $hidden = [
        'updated_at' ,'year'
    ];

    protected $appends = [
        'level' ,'year_number'
    ];

    public function getLevelAttribute()
    {
        if($this->value >= 91 && $this->value <= 100) {
            return "优秀";
        }elseif ($this->value >= 81 && $this->value <= 90) {
            return "良好";
        }elseif ($this->value >= 71 && $this->value <= 80){
            return "中等";
        }elseif ($this->value >= 61 && $this->value <= 70){
            return "一般";
        }else{
            return "差";
        }
    }

    public function getYearNumberAttribute()
    {
        return ($this->year ? $this->year->value : '');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function year()
    {
        return $this->belongsTo(Year::class);
    }
}
